﻿using System;
using System.Collections;
using System.Text;

namespace __Ts.Core.ConfigTable
{
    public class ConfigTableHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configTable"></param>
        /// <returns></returns>
        public static ConfigTableAttribute GetAttribute(Type configTable)
        {
            object[] o = configTable.GetCustomAttributes(typeof(ConfigTableAttribute), false);
            if (o.Length > 0)
                return o[0] as ConfigTableAttribute;
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        public class VersionConverter
        {
            /// <summary>
            /// 
            /// </summary>
            /// <param name="version"></param>
            /// <returns></returns>
            public static string ConvertToString(int version)
            {
                return string.Format("{0}.{1}.{2}.{3}",
                    version / 1000000,
                    version % 1000000 / 10000,
                    version % 10000 / 100,
                    version % 100
                    );
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="version"></param>
            /// <returns></returns>
            public static int ConvertToInt(string version)
            {
                if (string.IsNullOrEmpty(version))
                    return 0;

                StringBuilder sb = new StringBuilder();
                IEnumerator et = version.Split('.').GetEnumerator();
                int i = 0;
                while (et.MoveNext())
                {
                    i = Convert.ToInt32(et.Current);
                    if (i < 10)
                        sb.Append("0").Append(i);
                    else
                        sb.Append(i);
                }
                return Convert.ToInt32(sb.ToString());
            }
        }
    }
}
